package com.bishe.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bishe.entity.ClaTeacher;
import com.bishe.entity.Comment;
import com.bishe.entity.resp.CommonPageRequest;
import com.bishe.entity.resp.CommonSortOrderEnum;
import com.bishe.entity.resp.PageParam;
import com.bishe.mapper.ClaTeacherMapper;
import com.bishe.mapper.CommentMapper;
import com.bishe.service.ClaTeacherService;
import com.bishe.service.CommentService;
import org.springframework.stereotype.Service;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
        implements CommentService {
    @Override
    public Page<Comment> page(PageParam pageParam) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();

        if (ObjectUtil.isAllNotEmpty(pageParam.getSearchKey(), pageParam.getSearchValue())) {
            queryWrapper.eq(true, StrUtil.toUnderlineCase(pageParam.getSearchKey()), pageParam.getSearchValue());
        }

        if(ObjectUtil.isAllNotEmpty(pageParam.getSortField(), pageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(pageParam.getSortOrder());
            queryWrapper.orderBy(true, pageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(pageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(Comment::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }
}
